package com.enterprise.controller.api.log;

import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.entity.Visitlog;
import com.enterprise.service.LogService;
import com.enterprise.util.AddressUtil;
import com.enterprise.util.RequestHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@Slf4j
@RequestMapping("/api/log")
@Controller("apiLogController")
public class LogController {

    @Autowired
    LogService logService;

    @ResponseBody
    @RequestMapping("/savelog")
    public BaseResponse<Visitlog> savelog(HttpServletRequest request){
        Visitlog visitlog = new Visitlog();
        String visitIP = AddressUtil.getIp(RequestHolder.getRequest());
        visitlog.setVisitIP(visitIP);
        String visittime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        visitlog.setVisittime(visittime);
        Visitlog visitlog1 = logService.selectOne(visitlog);
        if (visitlog1 == null){
            log.info("savelog ip :{}",visitIP);
            logService.insert(visitlog);
        }
        BaseResponse baseResponse =  new BaseResponse();
        return baseResponse;
    }

}
